/*
Hey you! You are reading my code!
I want to say that my code is far from perfect, and if you see that I'm doing something
in a really wrong/dumb way, please give me advices instead of saying "LOL U BAD CODER"
        Thanks
      - MadJawa
*/
AddCSLuaFile( "cl_init.lua" );
AddCSLuaFile( "shared.lua" );

resource.AddFile( "models/madjawa/laser_reflector.dx80.vtx" );
resource.AddFile( "models/madjawa/laser_reflector.dx90.vtx" );
resource.AddFile( "models/madjawa/laser_reflector.mdl" );
resource.AddFile( "models/madjawa/laser_reflector.phy" );
resource.AddFile( "models/madjawa/laser_reflector.sw.vtx" );
resource.AddFile( "models/madjawa/laser_reflector.vvd" );

resource.AddFile( "materials/VGUI/entities/gmod_laser_reflector.vtf" );
resource.AddFile( "materials/VGUI/entities/gmod_laser_reflector.vmt" );

include( "shared.lua" );


function ENT:SpawnFunction( ply, tr )
   
 	if not tr.Hit then return; end
 	 
 	local SpawnPos = tr.HitPos + tr.HitNormal * 35;
 	 
 	local ent = ents.Create( "gmod_laser_converter" );
	ent:SetModel( "models/Combine_Helicopter/helicopter_bomb01.mdl" );
	ent:SetPos( SpawnPos );
 	ent:Spawn();
 	ent:Activate();
	ent:SetAngles( Angle( 0, ( ply:GetAimVector():Angle().y + 180 ) % 360, 0 ) ); -- Sets the right angle at spawn. Thanks to aVoN!
	
	ent:SetMaterial("models/props_lab/xencrystal_sheet");
 	 
	return ent;
 	 
end


function ENT:Initialize()

	self.Entity:PhysicsInit( SOLID_VPHYSICS );
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS );
	self.Entity:SetSolid( SOLID_VPHYSICS );
	
	local phys = self.Entity:GetPhysicsObject();
	if ( phys:IsValid() ) then
		phys:Wake();
	end
	
	self.Targets = {};
	self.Hits = {};
	self.LaserConvert = 1
	
	self:SetBeamLength( 0 );
	self:SetRateAmmount( 0 );
	self:SetBeamWidth( 0, true );
	self:SetDamageAmmount( 0 );
	self:SetBeamMaterial( "cable/physbeam" );
	
	
	if WireAddon then
		self.Outputs = Wire_CreateOutputs( self.Entity, { "Converting"} );
	end
        RD_Register(self, true)--For Environments Compatibility

end


function ENT:Think()

	local updateCrystal;
	
	for k,v in pairs(self.Hits) do
		local lastTarget;
		if ValidEntity(v) then lastTarget = v.Targets[#v.Targets];
		else lastTarget = nil; end
		
		if (not lastTarget or lastTarget ~= self) then
			table.remove(self.Hits, k);
			updateCrystal = true;
		elseif (ValidEntity(v) and v.IsModified) then
			updateCrystal = true;
			v.IsModified = false;
		end
	end
	
	if updateCrystal then self:UpdateLaserProperties(); end
	
	if (#self.Hits > 0) then self:SetOn(true); else self:SetOn(false); self.Hits = {}; end

	if WireAddon then Wire_TriggerOutput( self.Entity, "Converting", #self.Hits ); end
	
	if ( self:GetOn() ) then
		self:SupplyResource("energy", self:GetRateAmmount())
	end
	
	self.Entity:NextThink( CurTime() );

	return true;

end



function ENT:UpdateLaserProperties()
	if (#self.Hits > 0) then
		for k,v in pairs(self.Hits) do
			local laserRate = v:GetRateAmmount();
			rate = rate + laserRate;
		end
		
		self:SetRateAmmount(rate);
		self.IsModified = true;
	end
end




